/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.home.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.Interval;
import org.joda.time.Period;

/**
 *
 * @author pnduy
 */
public class DateUtils {
    
    public static int getCurrentWeekOfYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.WEEK_OF_YEAR);
    }
    
    public static int getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }
    
    public static int getCurrentMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1; // +1 because January  = 0
    }
    
    public static Map<Integer, Date[]> getMapWeekInCurrentYear() {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        return getMapWeekInYear(currentYear);
    }
    
    public static Map<Integer, Date[]> getMapWeekInYear(int year) {
        
        Period weekPeriod = new Period().withWeeks(1);
        DateTime startDate = new DateTime(year, 1, 1, 0, 0, 0, 0);
        while (startDate.getDayOfWeek() != DateTimeConstants.MONDAY) {
            startDate = startDate.plusDays(1);
        }

        DateTime endDate = new DateTime(year + 1, 1, 1, 0, 0, 0, 0);
        Interval i = new Interval(startDate, weekPeriod);
        Map<Integer, Date[]> mapWeekFromTo = new TreeMap<>();
        Calendar cal = Calendar.getInstance();
        int k = 0;
        while (i.getStart().isBefore(endDate)) {           
            Date[] dateFromAndTo = new Date[2];
            dateFromAndTo[0] = i.getStart().toDate();
            dateFromAndTo[1] = i.getEnd().minusMillis(1).toDate();
            
            // check last week
            cal.setTime(dateFromAndTo[1]);
            if (cal.get(Calendar.YEAR) > year) {
                dateFromAndTo[1] = new DateTime(year, 12, 31, 0, 0).toDate();
                mapWeekFromTo.put(k + 1, dateFromAndTo);
                break;
            }
            k = i.getStart().getWeekOfWeekyear();
            mapWeekFromTo.put(k, dateFromAndTo);            
            i = new Interval(i.getStart().plus(weekPeriod), weekPeriod);
        }
        Date stD = mapWeekFromTo.get(2)[0];
        cal.setTime(stD);
        Calendar cal2 = Calendar.getInstance();
        cal2.set(year, 1, 7);
        // check first week
        if (cal.getTime().before(cal2.getTime())) {
            Date[] dateFromAndTo = new Date[2];
            DateTime dt = new DateTime(mapWeekFromTo.get(2)[0]);
            dateFromAndTo[0] = new DateTime(year, 1, 1, 0, 0).toDate();
            dateFromAndTo[1] = dt.minusDays(1).toDate();
            mapWeekFromTo.put(1, dateFromAndTo);
        }        
        return mapWeekFromTo;
    }
    
    public static int getWeekByDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.WEEK_OF_YEAR);
    }
    
    public static int getQuarterByMonth(int month) {
        return month % 3 == 0?  (month / 3): ( month / 3)+1;
    }
}
